package com.bridge.zookeeper.watcher;

import com.bridge.enums.EnvEnum;
import com.bridge.enums.NeedUpdateEnum;
import com.bridge.processor.init.cache.container.LocalCacheHolder;
import com.bridge.zookeeper.BridgeNodeManager;
import com.bridge.zookeeper.data.ConfigKeyNodeData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Jay
 * @version v1.0
 * @description 根据环境订阅配置项
 * @date 2019-03-27 17:47
 */
@Slf4j
public class RegisterNodeTask {


    /**
     * 根据环境订阅配置项，即注册临时节点
     *
     * @param envEnum
     */
    public static void doRegisterTask(EnvEnum envEnum) {
        ConcurrentHashMap<String, ConfigKeyNodeData> map = LocalCacheHolder.getCacheHolder();
        if (!CollectionUtils.isEmpty(map)) {
            map.forEach((s, configKeyNode) -> {
                String key = configKeyNode.getKey();
                BridgeNodeManager.getInstance()
                        .registerMachineNode(key, configKeyNode, NeedUpdateEnum.NOT_NEED_UPDATE.getKey(), envEnum);
            });
        }
    }
}
